
import os, re
from zhengwuinfos.conf import settings
from zhengwuinfos.BaseInfos import BaseInfos


class Theme(BaseInfos):
    """
    按主题查看类
    """
    def handle_theme_resp(self, resp):
        ''' 处理第一次穿透时的数据信息 '''
        dir_name_list = []
        all_theme_infos_list = []
        channelName = resp['results']['channelName']  # 按主题查看
        child_datas = resp['results']['children']
        dir_name_list.append(channelName)
        # 循环主题每个标题下的数据信息：主题码channelCode 主题名称channelName 主题idchannelId 该主题数据总条数docCount
        for i in range(0, len(child_datas)):
            child_dics = {}
            son_chanCode = resp['results']['children'][i]['channelCode']
            son_chanName = resp['results']['children'][i]['channelName']
            son_chanId = resp['results']['children'][i]['channelId']
            son_docCount = resp['results']['children'][i]['docCount']
            son_isChildren = child_datas[i]['isChildren']
            child_dics['son_chanCode'] = son_chanCode
            child_dics['son_chanName'] = son_chanName
            child_dics['son_chanId'] = son_chanId
            child_dics['son_docCount'] = son_docCount
            child_dics['channelName'] = channelName
            child_dics['son_isChildren'] = son_isChildren
            all_theme_infos_list.append(child_dics)
        return dir_name_list, all_theme_infos_list

    def theme_cata_list_datas(self):
        """ 获取主题目录标题列表数据 和 路径名称数据"""
        resp = super().send_reqs(settings.url_topic)
        dir_name_list, all_theme_infos_list = self.handle_theme_resp(resp)
        return dir_name_list, all_theme_infos_list

    def handle_datas(self, resp):
        """ 获取单页所有条数的数据 """
        one_page_data_list = []
        for i in range(0, len(resp['data']['results'])):
            rows_dics = {}
            # 专题
            channelName = resp['data']['results'][i]['channelName']
            # 标题
            title = resp['data']['results'][i]['title']
            # 子标题
            subTitle = resp['data']['results'][i]['subTitle']
            # 内容
            # content = resp['data']['results'][i]['content']
            rstr = r"\&ensp\;\&ensp\;\&ensp\;"
            content = re.sub(rstr, "\n\t", resp['data']['results'][i]['content'])
            # 发布时间
            pubTime = resp['data']['results'][i]['publishedTimeStr']
            # # 文件下
            rows_dics['channelName'] = channelName
            rows_dics['title'] = title
            rows_dics['subTitle'] = subTitle
            rows_dics['content'] = content
            rows_dics['pubTime'] = pubTime
            one_page_data_list.append(rows_dics)
        return one_page_data_list

    def handle_all_datas(self, page_cnts, *args):
        """ 索取所有页面的所有数据 """
        # 获取下一级专题标题数据列表
        if args[0]:
            url = settings.url_child.format(args[1])
            resp = super().send_reqs(url)
            child_data = resp['results']['children']
            channelName_list = []
            for i in range(0, len(child_data)):
                if i == 0:
                    fir_channelName = resp['results']['channelName']
                channelName_list.append('\\'.join(list((fir_channelName, child_data[i]['channelName'],))))
            dir_name_list, all_theme_infos_list = self.handle_theme_resp(resp)
            all_titles_list = self.judge_ischildren(all_theme_infos_list)
            return channelName_list, all_titles_list
        # 没有下一级，直接爬取页面最后一级数据。   说明：无论穿透多少级，最后一定是走到这里
        else:
            all_datas_list = []
            num = 0
            for x in range(1, int(page_cnts) + 1):
                url = super().get_url(x, args[1])
                resp = super().send_reqs(url)
                one_page_data_list = self.handle_datas(resp)
                for y in range(0, len(one_page_data_list)):
                    num += 1
                    all_datas_list.append(one_page_data_list[y])
                    print('{} 第 {} 页 第 {} 条数据: {} 爬取成功 ^_^'.format(args[2], x, y + 1, one_page_data_list[y]))
            print('## 共 {} 条数据爬取完成 {} '.format(len(all_datas_list), settings.str_infos[0]))
            return all_datas_list

    def judge_ischildren(self, all_theme_infos_list):
        """ 判断 isChildren 是否为faslse，若为false直接获取该主题的所有数据信息，若不为false，则继续获取下一级主题标题名称列表 """
        all_titles_datas_list = []
        # 循环下一级专题标题
        for i in range(0, len(all_theme_infos_list)):
            son_isChildren = all_theme_infos_list[i]['son_isChildren']
            son_chanName = all_theme_infos_list[i]['son_chanName']
            channelName = all_theme_infos_list[i]['channelName']
            dir_name = channelName + '\\' + son_chanName
            # 判断 isChildren为 False，进入数据处理函数 handle_all_datas，直接获取最后一级数据
            if not son_isChildren:
                page_cnts = self.get_page_cnts(all_theme_infos_list[i]['son_docCount'])
                all_nodatas_list = self.handle_all_datas(page_cnts, son_isChildren, all_theme_infos_list[i]['son_chanId'], dir_name)
                all_titles_datas_list.append(all_nodatas_list)
            # 判断 isChildren为 True，进入数据处理函数 handle_all_datas，获取下一级标题列表，继续穿透
            else:
                son_chanCode = all_theme_infos_list[i]['son_chanCode']
                son_chanName = all_theme_infos_list[i]['son_chanName']
                son_isChildren = all_theme_infos_list[i]['son_isChildren']
                # 获取下一级专题标题的总页码数
                page_cnts = self.get_page_cnts(all_theme_infos_list[i]['son_docCount'])
                channelName_list, all_yestitles_list = self.handle_all_datas(page_cnts, son_isChildren, son_chanCode, dir_name)
                all_titles_datas_list.append(channelName_list)
                all_titles_datas_list.append(all_yestitles_list)
        return all_titles_datas_list

    def verif_dirs(self, dir_name_list, all_datas_list):
        ''' 校验穿透n层的路径是否存在，不存在则创建 '''
        for i in range(0, len(all_datas_list)):
            if i % 2 == 0:
                for j in range(0, len(all_datas_list[i])):
                    dir_name = os.getcwd() + '\\zhengwuinfos\\files\\' + dir_name_list[0] + '\\' + all_datas_list[i][j]
                    if not os.path.exists(dir_name):
                        os.makedirs(dir_name)

    def handle_all_datas_list(self, all_datas_list):
        ''' 处理穿透n层知道最后一级返回的所有数据信息 '''
        tmp_dirs_list = []
        tmp_datas_list = []
        dirs_list = []
        datas_list = []
        for i in range(0, len(all_datas_list)):
            if i % 2 == 0:
                tmp_dirs_list.append(all_datas_list[i])
            else:
                tmp_datas_list.append(all_datas_list[i])
        for j in range(0, len(tmp_dirs_list)):
            [dirs_list.append(tmp_dirs_list[j][k]) for k in range(0, len(tmp_dirs_list[j]))]
        for x in range(0, len(tmp_datas_list)):
            [datas_list.append(tmp_datas_list[x][y]) for y in range(0, len(tmp_datas_list[x]))]
        return dirs_list, datas_list

    def save_files(self, dir_name_list, all_datas_list):
        # 将处理后的所有穿透n层的数据保存到相对应的路径下
        dirs_list, datas_list = self.handle_all_datas_list(all_datas_list)
        for i in range(0, len(dirs_list)):
            if not datas_list[i]:
                continue
            for j in range(0, len(datas_list[i])):
                title = datas_list[i][j]['title']
                subTitle = datas_list[i][j]['subTitle']
                content = datas_list[i][j]['content']
                pubTime = datas_list[i][j]['pubTime']
                file_name = os.getcwd() + '\\zhengwuinfos\\files\\' + dir_name_list[0] + '\\' + dirs_list[i] + '\\' + title + '.doc'
                self.verif_dirs(dir_name_list, all_datas_list)
                with open(file_name, 'w', encoding='utf-8') as fp:
                    fp.write('\t\t\t\t{}\t\t\t\t\n'.format(title))
                    fp.write('\t\t\t\t\t\t\t发布时间：{}\t\t\n'.format(pubTime))
                    fp.write('\t' + subTitle + '\n')
                    fp.write('\t' + content)
                print('Success {} 文件已保存在 {} 路径下！！！'.format(title, os.getcwd() + '\\files\\' + dir_name_list[0] + '\\' + dirs_list[i] + '\\'))

    def themes_main(self):
        dir_name_list, all_theme_infos_list = self.theme_cata_list_datas()
        all_datas_list = self.judge_ischildren(all_theme_infos_list)
        self.save_files(dir_name_list, all_datas_list)


# theme = Theme()
# theme.themes_main()
